const Generator = require('yeoman-generator');
const fs = require('fs');
const path = require('path');
 
function walk(basePath, fileList = []) {

	var dirList = fs.readdirSync(basePath);
	dirList.forEach(function(item) {
    fileName = path.join(basePath, item)
		if (fs.statSync(fileName).isDirectory()) {
			walk(fileName, fileList);
		} else {
			fileList.push(fileName);
		}
  });
  return fileList
}
 

module.exports = class extends Generator {
  prompting() {
    return this.prompt([
      {
        type: 'input',
        name: 'appname',
        message: 'your project name',
        default: this.appname,
      }
    ])
      .then(answer => {
        this.answer = answer
      })
  }
  writing() {
    const templatesPath = path.join(__dirname, 'templates')
    const templates = walk(templatesPath)
    // console.log(templates);

    for (const tmpl of templates) {
      const output = this.destinationPath(tmpl.slice(templatesPath.length+1))
      const context = this.answer

      this.fs.copyTpl(tmpl, output, context)
    }

  }
}
